// ml:run = time -p $bin < input
#include <cstdio>

template <class T>
void scan(T & a)
{
	a = 0;
	bool flag = false;
	char ch = std::getchar();
	if (ch == '-')
		flag = true;
	else if (ch >= '0' && ch <= '9')
		a = ch - '0';

	while ((ch = std::getchar()) >= '0' && ch <= '9')
		a = a * 10 + ch - '0';
	if (flag) a = -a;
}

int const maxn = 1000007;
int a[maxn];
int b[maxn];
int next[maxn];
int da[maxn];
int n, m, p, len;
bool calc_next;

void get_next()
{
    int k=-1,j=0;
    next[0]=-1;
    while(j<m)
    {
        if(k==-1||b[j]==b[k])
        {
            j++;k++;
            next[j]=k;
        }
        else
        k=next[k];
    }
}
int kmp()
{
    int i=0,j=0;
    get_next();
    int sum = 0;
    while(j<m&&i<len)
    {
            if(j==-1||a[i]==b[j])
            {
                i++;j++;
            }
            else j=next[j];
            if(j==m)
            {
                sum++;j=next[j];
            }
    }
    return sum;
}
int main()
{
    /* std::ios_base::sync_with_stdio(false); */
    int T; scan(T);
    /* std::cin >> T; */
    for (int ti = 1; ti <= T; ti++) {
        printf("Case #%d: ", ti);
        /* std::cout << "Case #" << ti << ": "; */
        scan(n); scan(m); scan(p);
        /* std::cin >> n >> m >> p; */
        for (int i = 0; i < n; i++) scan(da[i]);
            /* std::cin >> da[i]; */
        for (int i = 0; i < m; i++) scan(b[i]);
            /* std::cin >> b[i]; */
        if (n / p + 1 < m) {
            printf("0\n");
            continue;
        }
        int ans = 0;
        calc_next = false;
        for (int i = 0; i < p; i++) {
            len = 0;
            int tm = (n - i) / p + 1;
            if (m > tm) break;
            for (int j = i; j < n; j += p) a[len++] = da[j];
            ans += kmp();
        }
        printf("%d\n", ans);
        /* std::cout << ans << "\n"; */
    }
}

